package com.hightemperator.util;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * _ooOoo_
 * o8888888o
 * 88" . "88
 * (| -_- |)
 * O\  =  /O
 * ____/`---'\____
 * .'  \\|     |//  `.
 * /  \\|||  :  |||//  \
 * /  _||||| -:- |||||-  \
 * |   | \\\  -  /// |   |
 * | \_|  ''\---/''  |   |
 * \  .-\__  `-`  ___/-. /
 * ___`. .'  /--.--\  `. . __
 * ."" '<  `.___\_<|>_/___.'  >'"".
 * | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 * \  \ `-.   \_ __\ /__ _/   .-` /  /
 * ======`-.____`-.___\_____/___.-`____.-'======
 * `=---='
 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 * 佛祖保佑       永无BUG
 *
 * @Author: wzd
 * @Date: 2017/12/5 13:19
 * @Description: 移动端响应信息
 */
public class LocalResponseBody<T> implements Serializable {
    private String responseState; // 0失败,1成功
    private String msg;
    private String count;
    private T data;

    public LocalResponseBody() {
    }

    public LocalResponseBody(String responseState, String msg, String count, T data) {
        this.responseState = responseState;
        this.msg = msg;
        this.count = count;
        this.data = data;
    }

    public LocalResponseBody(String responseState, String msg, T data) {
        this.responseState = responseState;
        this.msg = msg;
        this.data = data;
    }

//    public LocalResponseBody(String responseState, String msg) {
//        this.responseState = responseState;
//        this.msg = msg;
//    }

    public LocalResponseBody(String msg, String count) {
        this.count = count;
        this.msg = msg;
    }

    public LocalResponseBody(String msg) {
        this.msg = msg;
    }

    public String getResponseState() {
        return responseState;
    }

    public void setResponseState(String responseState) {
        this.responseState = responseState;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getCount() {
        return count;
    }

    public void setCount(String count) {
        this.count = count;
    }

    public T getData() {
        return data;

    }

    public void setData(T data) {
        this.data = data;
    }

    public LocalResponseBody outTime(T t) {
        if (t != null) {
            if (msg != null) return new LocalResponseBody("0", msg, t);
            return new LocalResponseBody("-1", "操作超时", t);
        } else {
            if (msg != null) return new LocalResponseBody("0", msg, new HashMap<String, Object>());
            return new LocalResponseBody("-1", "操作超时", new HashMap<String, Object>());
        }
    }

    public LocalResponseBody illegalRequest(T t) {
        if (t != null) {
            if (msg != null) return new LocalResponseBody("0", msg, t);
            return new LocalResponseBody("-2", "非法请求", t);
        } else {
            if (msg != null) return new LocalResponseBody("0", msg, new HashMap<String, Object>());
            return new LocalResponseBody("-2", "非法请求", new HashMap<String, Object>());
        }
    }

    public LocalResponseBody doFail(T t) {
        if (t != null) {
            if (msg != null) {
                if (!t.getClass().isArray() && !(t instanceof List)) {
                    List l = new ArrayList();
                    l.add(t);
                    return new LocalResponseBody("0", msg, l);
                }
                return new LocalResponseBody("0", msg, t);
            }
            if (!t.getClass().isArray() && !(t instanceof List)) {
                List l = new ArrayList();
                l.add(t);
                return new LocalResponseBody("0", "操作失败", l);
            }
            return new LocalResponseBody("0", "操作失败", t);
        } else {
            if (msg != null) return new LocalResponseBody("0", msg, new ArrayList<String>());
            return new LocalResponseBody("0", "操作失败", new ArrayList<String>());
        }
    }

    public LocalResponseBody doFail() {
        if (msg != null) return new LocalResponseBody("0", msg, new ArrayList<String>());
        return new LocalResponseBody("0", "操作失败", new ArrayList<String>());
    }

    //该方法内注释的是 请求之后的返回格式（不注释返回的所有数据都是数组，注释了 根据前面参数的类型来确定是对象还是数组）
    public LocalResponseBody doSuccess(T t) {
        if (t != null) {
            if (msg != null) {
//                if(!t.getClass().isArray() &&  !(t instanceof List)){
//                    List l =  new ArrayList();
//                    l.add(t);
//                    return new LocalResponseBody("1", msg,l);
//                }
                if (count != null) {
                    return new LocalResponseBody("1", msg, count, t);
                }
                return new LocalResponseBody("1", msg, t);
            }
//            if(!t.getClass().isArray() &&  !(t instanceof List)){
//                List l =  new ArrayList();
//                l.add(t);
//                return new LocalResponseBody("1",  "操作成功",l);
//            }
            return new LocalResponseBody("1", "操作成功", t);
        } else {
            if (msg != null) return new LocalResponseBody("1", msg, new ArrayList<String>());
            return new LocalResponseBody("1", "操作成功", new ArrayList<String>());
        }
    }

    public LocalResponseBody doSuccess() {
        if (msg != null) return new LocalResponseBody("1", msg, new ArrayList<String>());
        return new LocalResponseBody("1", "操作成功", new ArrayList<String>());
    }

    public LocalResponseBody noAuth() {
        if (msg != null) return new LocalResponseBody("-1", msg, new ArrayList<String>());
        return new LocalResponseBody("-1", "操作失败,身份验证失败", new ArrayList<String>());
    }


}
